//
//  Status.swift
//  Weibo
//
//  Created by 张远文 on 16/4/29.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit

class Status: NSObject {

    //微博创建时间
    var created_at: String?
    
    //微博ID
    var id: Int = 0
    
    //微博内容
    var text: String?
    
    //微博来源
    var source: String?
    
    //定义User属性
    var user: User?
    
    //配图视图数组
    var pic_urls: [[String : String]]?
    
    //计算型属性
    var imageURLs: [NSURL]? {
        
        guard let urls = pic_urls else {
            return nil
        }
        
        var tempArray = [NSURL]()
        for item in urls {
            
            let url = NSURL(string: item["thumbnail_pic"]!)
            tempArray.append(url!)
            
        }
        return tempArray
    }
    
    var pictureURLs: [NSURL]? {
        //如果转发微博不为空
        if retweeted_status != nil {
            return retweeted_status?.imageURLs
        }
        //原创微博
        return imageURLs
    }
    
    
    //被转发的原创微博片段
    var retweeted_status: Status?
    
    //计算获取谁的源
    var weiboSource: String? {
        if retweeted_status != nil {
            return retweeted_status?.source
        }
        
        return source
    }
    
    //kvc字典转模型初始化方法
    init(dict: [String : AnyObject]) {
        
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            
            if let dict = value as? [String : AnyObject] {
                user = User(dict: dict)
            }
            return
        }
        
        //把被转发的原创微博 字典转模型
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = Status(dict: dict)
            }
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    //过滤掉没有用到的字段
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    
    
    
    
    
    
    
    
    
}
